#include "MyPacketProcessor.h"
#include "MyURB.h"
#include "MyIOCTL.h"

extern QAction *actionFilters[35];

const char *PacketNames[] =
{
    "IOCTL_INTERNAL_USB_CYCLE_PORT",
    "IOCTL_INTERNAL_USB_GET_BUS_INFO",
    "IOCTL_INTERNAL_USB_GET_CONTROLLER_NAME",
    "IOCTL_INTERNAL_USB_GET_DEVICE_CONFIG_INFO",
    "IOCTL_INTERNAL_USB_GET_HUB_NAME",
    "IOCTL_INTERNAL_USB_GET_PORT_STATUS",
    "IOCTL_INTERNAL_USB_GET_TOPOLOGY_ADDRESS",
    "IOCTL_INTERNAL_USB_RESET_PORT",
    "IOCTL_INTERNAL_USB_SUBMIT_IDLE_NOTIFICATION",
    "IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION",
    "IOCTL_USB_GET_HUB_CAPABILITIES",
    "IOCTL_USB_GET_HUB_CAPABILITIES_EX",
    "IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME",
    "IOCTL_USB_GET_NODE_CONNECTION_INFORMATION",
    "IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX",
    "IOCTL_USB_GET_NODE_CONNECTION_NAME",
    "IOCTL_USB_GET_NODE_INFORMATION",
    "IOCTL_USB_GET_ROOT_HUB_NAME",
    "IOCTL_USB_HUB_CYCLE_PORT",
    "IOCTL_USB_USER_REQUEST",

    "URB_SELECT_INTERFACE",
    "URB_SELECT_CONFIGURATION",
    "URB_PIPE_REQUEST",
    "URB_GET_CURRENT_FRAME_NUMBER",
    "URB_CONTROL_TRANSFER",
    "URB_CONTROL_TRANSFER_EX",
    "URB_BULK_OR_INTERRUPT_TRANSFER",
    "URB_ISOCH_TRANSFER",
    "URB_CONTROL_DESCRIPTOR_REQUEST",
    "URB_CONTROL_GET_STATUS_REQUEST",
    "URB_CONTROL_FEATURE_REQUEST",
    "URB_CONTROL_VENDOR_OR_CLASS_REQUEST",
    "URB_CONTROL_GET_INTERFACE_REQUEST",
    "URB_CONTROL_GET_CONFIGURATION_REQUEST",
    "URB_OS_FEATURE_DESCRIPTOR_REQUEST"
};

MyPacket *ProcessIncomingBuffer(void *buf, unsigned long bufSize)
{
    MyBuffer buffer(buf, bufSize);	
	unsigned char packetType;
	MyPacket *pPacket = NULL;

	buffer.Read(&packetType, 1);
	bufSize -= 1;

	void *pBuffer = buffer.GetCurrAddr();

	if (!actionFilters[packetType]->isChecked())
	{
		return NULL;
	}	

    switch (packetType)
    {
    case PACK_IOCTL_INTERNAL_USB_GET_BUS_INFO:
        {
            pPacket = new MY_IOCTL_INTERNAL_USB_GET_BUS_INFO;
            pPacket->Deserialize(pBuffer, bufSize, packetType);
        }
        break;

    case PACK_IOCTL_INTERNAL_USB_GET_CONTROLLER_NAME:
        {
            pPacket = new MY_IOCTL_INTERNAL_USB_GET_CONTROLLER_NAME;
            pPacket->Deserialize(pBuffer, bufSize, packetType);
        }
        break;

    case PACK_IOCTL_INTERNAL_USB_GET_PORT_STATUS:
        {
            pPacket = new MY_IOCTL_INTERNAL_USB_GET_PORT_STATUS;
            pPacket->Deserialize(pBuffer, bufSize, packetType);
        }
        break;

    case PACK_IOCTL_USB_GET_HUB_CAPABILITIES:
        {
            pPacket = new MY_IOCTL_USB_GET_HUB_CAPABILITIES;
            pPacket->Deserialize(pBuffer, bufSize, packetType);
        }
        break;

    case PACK_IOCTL_USB_GET_HUB_CAPABILITIES_EX:
        {
            pPacket = new MY_IOCTL_USB_GET_HUB_CAPABILITIES_EX;
            pPacket->Deserialize(pBuffer, bufSize, packetType);
        }
        break;

    case PACK_IOCTL_INTERNAL_USB_GET_DEVICE_CONFIG_INFO:
        {
            pPacket = new MY_IOCTL_INTERNAL_USB_GET_DEVICE_CONFIG_INFO;
            pPacket->Deserialize(pBuffer, bufSize, packetType);
        }
        break;

    case PACK_IOCTL_INTERNAL_USB_GET_TOPOLOGY_ADDRESS:
        {
            pPacket = new MY_IOCTL_INTERNAL_USB_GET_TOPOLOGY_ADDRESS;
            pPacket->Deserialize(pBuffer, bufSize, packetType);
        }
        break;

    case PACK_IOCTL_USB_GET_NODE_CONNECTION_INFORMATION:
        {
            pPacket = new MY_IOCTL_USB_GET_NODE_CONNECTION_INFORMATION;
            pPacket->Deserialize(pBuffer, bufSize, packetType);
        }
        break;

    case PACK_IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX:
        {
            pPacket = new MY_IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX;
            pPacket->Deserialize(pBuffer, bufSize, packetType);
        }
        break;

    case PACK_IOCTL_USB_GET_NODE_CONNECTION_NAME:
        {
            pPacket = new MY_IOCTL_USB_GET_NODE_CONNECTION_NAME;
            pPacket->Deserialize(pBuffer, bufSize, packetType);
        }
        break;

    case PACK_IOCTL_USB_GET_NODE_INFORMATION:
        {
            pPacket = new MY_IOCTL_USB_GET_NODE_INFORMATION;
            pPacket->Deserialize(pBuffer, bufSize, packetType);
        }
        break;

    case PACK_IOCTL_INTERNAL_USB_CYCLE_PORT:
    case PACK_IOCTL_INTERNAL_USB_GET_HUB_NAME:
    case PACK_IOCTL_INTERNAL_USB_RESET_PORT:
    case PACK_IOCTL_INTERNAL_USB_SUBMIT_IDLE_NOTIFICATION:
    case PACK_IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION:
    case PACK_IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME:
    case PACK_IOCTL_USB_GET_ROOT_HUB_NAME:
    case PACK_IOCTL_USB_HUB_CYCLE_PORT:
    case PACK_IOCTL_USB_USER_REQUEST:
        {
            pPacket = new MY_IOCTL_EMPTY;
            pPacket->DeserializeEmpty(pBuffer, bufSize, packetType);
        }
        break;
    
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////


    case PACK_URB_SELECT_INTERFACE:
        {
            pPacket = new MY_URB_SELECT_INTERFACE;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_SELECT_CONFIGURATION:
        {
            pPacket = new MY_URB_SELECT_CONFIGURATION;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_PIPE_REQUEST:
        {
            pPacket = new MY_URB_PIPE_REQUEST;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_GET_CURRENT_FRAME_NUMBER:
        {
            pPacket = new MY_URB_GET_CURRENT_FRAME_NUMBER;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_CONTROL_TRANSFER:
        {
            pPacket = new MY_URB_CONTROL_TRANSFER;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_CONTROL_TRANSFER_EX:
        {
            pPacket = new MY_URB_CONTROL_TRANSFER_EX;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_BULK_OR_INTERRUPT_TRANSFER:
        {
            pPacket = new MY_URB_BULK_OR_INTERRUPT_TRANSFER;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_ISOCH_TRANSFER:
        {
            pPacket = new MY_URB_ISOCH_TRANSFER;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_CONTROL_DESCRIPTOR_REQUEST:
        {
            pPacket = new MY_URB_CONTROL_DESCRIPTOR_REQUEST;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_CONTROL_GET_STATUS_REQUEST:
        {
            pPacket = new MY_URB_CONTROL_GET_STATUS_REQUEST;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_CONTROL_FEATURE_REQUEST:
        {
            pPacket = new MY_URB_CONTROL_FEATURE_REQUEST;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_CONTROL_VENDOR_OR_CLASS_REQUEST:
        {
            pPacket = new MY_URB_CONTROL_VENDOR_OR_CLASS_REQUEST;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_CONTROL_GET_INTERFACE_REQUEST:
        {
            pPacket = new MY_URB_CONTROL_GET_INTERFACE_REQUEST;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_CONTROL_GET_CONFIGURATION_REQUEST:
        {
            pPacket = new MY_URB_CONTROL_GET_CONFIGURATION_REQUEST;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    case PACK_URB_OS_FEATURE_DESCRIPTOR_REQUEST:
        {
            pPacket = new MY_URB_OS_FEATURE_DESCRIPTOR_REQUEST;
            pPacket->Deserialize(buffer.GetCurrAddr(), bufSize, packetType);
        }
        break;
    }

	return pPacket;
}
